<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
     * 后台商品添加
     */
    public function getAdd()
    {
        //调用分类列表的静态方法
        $goods = CateController::getCates();
    	return view('goods.add',['goods'=>$goods]);
    }

     /**
     * 后台商品添加数据
     */
    public function postInsert(Request $request)
    {
        $data = $request->except('_token');
        $res = DB::table('goods')->insert($data);
        //判断
        if($res){
            return redirect('/admin/goods/index')->with('success','添加成功');
        }else{
            return back()->with('error','添加失败');
        }
    }

    /**
     * 后台商品显示列表
     */
    public function getIndex(Request $request)
    {
        //获取分页大小
        $num = $request->input('num',10);
        //查询所有数据分页显示 判断
        if($request->input('keywords')){
            $goods = DB::table('goods')->where('trade_name','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
            $goods = DB::table('goods')->paginate($num);
        }
        //解析模板,分配变量
        return view('goods.index',['goods'=>$goods,'request'=>$request->all()]);
    }

    /**
     * 后台商品删除
     */
    public function getDelete(Request $request)
    {
        //获取id
        $id = $request->input('id');
        $res = DB::table('goods')->where('id',$id)->delete();
        if($res){
            return redirect('/admin/goods/index')->with('success','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

    /**
     * 后台商品修改
     */
    public function getEdit(Request $request)
    {
        //调用分类列表的静态方法
        $cates = CateController::getCates();
        //获取id
        $id = $request->input('id');
        $goods = DB::table('goods')->where('id',$id)->first();

        //解析模板  分配变量
        return view('goods.edit',['goods'=>$goods,'cates'=>$cates]);
    }

    /**
     * 修改商品数据
     */
    public function postUpdate(Request $request)
    {
        $data = $request->except('_token','id');
        $id = $request->input('id');
        $res = DB::table('goods')->where('id',$id)->update($data);
        //判断
        if($res){
            return redirect('/admin/goods/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    /**
    * 图片上传操作
    */
    public function upload(Request $request)
    {
        if($request->hasFile('pic')){
            //文件名称
            $name = md5(time()+rand(111,99999));
            //获取文件后缀名
            $suffix = $request->file('pic')->getClientOriginalExtension();
            $arr = ['jpg','png','gif','peng'];
            //判断上传文件类型
            if(!in_array($suffix,$arr)){
                echo '上传文件不符合要求';die;
            }
            //将指定文件移动到指定位置
            $request->file('pic')->move('./uploads/goods/',$name.'.'.$suffix);
            //将文件路径及文件名称返回
            return '/uploads/goods/'.$name.'.'.$suffix;
        }
    }
}
